package com.lfg.leetcode.leetcode200;

import com.lfg.leetcode.leetcode0.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class No222countNodes {

    public static void main(String[] args) {
        int ret = countNodes(TreeNode.stringToTreeNode("[1,2,3,4,5,6,7,8,9,10]"));
        System.out.println(ret);
    }

    public static int countNodes(TreeNode root) {
        int ret = 0;
        if (root == null) {
            return ret;
        }
        List<TreeNode> list = new ArrayList<>();
        list.add(root);
        while (!list.isEmpty()) {
            ret++;
            TreeNode node = list.remove(list.size() - 1);
            if (node.right != null) {
                list.add(node.right);
            }
            if (node.left != null) {
                list.add(node.left);
            }
        }

        return ret;
    }
}
